#include "TDBase.h"

MapPath::MapPath(void)
{
}

MapPath::~MapPath(void)
{
}

void MapPath::ConstructSegments (const StageMap& map)
{
	_segVerts.clear();
	_usedBrickIdx.resize(map.Size());
	_usedBrickIdx.reset();

	for (size_t i=0; i<_brickIdxQue.size()-1; i++)
	{
		_usedBrickIdx[_brickIdxQue[i]] = true;

		const MapBrick& brick = map[_brickIdxQue[i]];
		const MapBrick& nextBrick = map[_brickIdxQue[i+1]];
		
		vector3di midVert(
			(brick.Center().X +nextBrick.Center().X)/2,
			brick.Center().Y,
			(brick.Center().Z +nextBrick.Center().Z)/2
		);

		if (brick.Type() == BRICK_SLIDE)
		{
			midVert.Y = nextBrick.Center().Y;
		}

		_segVerts.push_back(midVert);
	}

	const MapBrick& lastBrick = map[_brickIdxQue.back()];
	_segVerts.push_back(lastBrick.Center());
}

int MapPath::Size() const 
{ 
	return _brickIdxQue.size(); 
}

int MapPath::operator[] (int idx) const 
{ 
	return _brickIdxQue[idx]; 
}

void MapPath::Clear() 
{ 
	_brickIdxQue.clear(); 
}

void MapPath::PushFront(int brickIdx) 
{ 
	_brickIdxQue.push_front(brickIdx); 
}

const vector3di& MapPath::VertAt(int idx) const 
{
	return _segVerts[idx]; 
}

int MapPath::EndBrickIdx() const
{
	return _brickIdxQue.back();
}

int MapPath::GetSegIdxOf (int brickIdx) const
{
	if (!_usedBrickIdx[brickIdx])
		return -1;

	for (int i=0; i<(int)_brickIdxQue.size(); i++)
	{
		if (_brickIdxQue[i] == brickIdx)
		{
			return i;
		}
	}

	assert (false);
	return -1;
}